package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljfiction.book.MyView.ContainsEmojiEditText;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class TomessageActivity extends BaseActivity {

    @BindView(R.id.updataemaliback)
    ImageView updataemaliback;
    @BindView(R.id.updataemaliok)
    TextView updataemaliok;
    @BindView(R.id.updataemali)
    ContainsEmojiEditText updataemali;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tomessage);
        ButterKnife.bind(this);
        if (!UserManageBean.userBean.getUserEmail().equals("未绑定邮箱")){
            updataemali.setText(UserManageBean.userBean.getUserEmail());
        }

    }

    @OnClick({R.id.updataemaliback, R.id.updataemaliok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.updataemaliback:
                goBack();
                break;
            case R.id.updataemaliok:
                if (!StringTool.isEmailtow(updataemali.getText().toString())){
                   showToast("请输入正确格式的邮箱");
                    return;
                }
                toemali();
                break;
        }
    }

    private void toemali() {
        OkHttpUtils.post(HttpManager.IP+"/person-updateInfo")

                .params("userEmail",updataemali.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            if (js.getInt("returnCode")==200){
                                UserManageBean.userBean.setUserEmail(updataemali.getText().toString());
                                showToast(js.getString("msg"));
                                goBack();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
